package util;

import model.tsp.Solution;

import java.util.List;

public class CheckUtils {

    public static void check(Solution solution, int[][] distance) {
        List<Integer> permutation = solution.getPermutation();
        int bestValue = solution.getCost();

        int value = printDistanceDetail(permutation, distance);
        assert value == bestValue : "value cannot match";

        for (int i = 0; i < permutation.size() - 1; i++) {
            for (int j = i + 1; j < permutation.size(); j++) {
                assert !permutation.get(i).equals(permutation.get(j)) : "element is duplicate";
            }
        }
    }

    private static int printDistanceDetail(List<Integer> permutation, int[][] distance) {
        String leftAlign = "| %-6s | %-6s | %-6s |%n";
        System.out.format("+--------------------------+%n");
        System.out.printf("| Distance Detail          |%n");
        System.out.format("+--------+-------+---------+%n");
        System.out.printf("| route  | dist  | cumSum |%n");
        System.out.format("+--------+-------+---------+%n");
        int dist = 0;
        int d;
        String p;
        int len = permutation.size();
        for (int i = 0; i < len - 1; i++) {
            d = distance[permutation.get(i)][permutation.get(i + 1)];
            dist += d;
            p = permutation.get(i) + "—>" + permutation.get(i + 1);
            System.out.format(leftAlign, p, d, dist);
        }
        d = distance[permutation.get(len - 1)][permutation.get(0)];
        dist += d;
        p = permutation.get(len - 1) + "—>" + permutation.get(0);
        System.out.format(leftAlign, p, d, dist);
        System.out.format("+--------+-------+---------+%n");
        System.out.println("total number of cities : " + len);
        System.out.println("total distance : " + dist);
        return dist;
    }


}
